package com.cloud.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cloud.common.domain.Result;
import com.cloud.common.enums.ApplyStatus;
import com.cloud.controller.BaseController;
import com.cloud.entity.SysApply;
import com.cloud.enums.BusinessType;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.user_info.service.ISysApplyService;
import com.cloud.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应用管理
 *
 * @author yzj
 * @since 2022-08-11
 */
@Tag(name = "应用管理")
@RestController
@RequestMapping("/system/apply")
public class SysApplyController extends BaseController {

    @Autowired
    private ISysApplyService sysApplyService;

    @Operation(summary = "查询应用管理列表")
    @RequestPermissions("system:apply:list")
    @GetMapping("/list")
    public Result list(SysApply sysApply) {
        sysApply.setUserId(SecurityUtils.getUserId());
        sysApply.setSysCode(SecurityUtils.getSysCode());
        return Result.success("查询成功", sysApplyService.selectSysApplyList(sysApply));
    }

    @Operation(summary = "新增应用管理信息")
    @Logs(title = "新增应用管理", businessType = BusinessType.INSERT)
    @RequestPermissions("system:apply:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysApply sysApply) {
        sysApply.setSysCode(SecurityUtils.getSysCode());
        return toResult(sysApplyService.insertObject(sysApply));
    }

    @Operation(summary = "修改应用管理信息")
    @Logs(title = "修改应用管理", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:apply:edit")
    @PostMapping("/update")
    public Result update(@RequestBody SysApply sysApply) {
        sysApply.setSysCode(SecurityUtils.getSysCode());
        return toResult(sysApplyService.updateObject(sysApply));
    }

    @Operation(summary = "查询应用管理详情")
    @RequestPermissions("system:apply:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        return Result.success("查询成功", sysApplyService.getObjectInfo(id));
    }

    @Operation(summary = "删除应用管理信息")
    @Logs(title = "删除应用管理", businessType = BusinessType.DELETE)
    @RequestPermissions("system:apply:remove")
    @PostMapping("/remove")
    public Result remove(String ids) {
        List<String> list = Arrays.asList(ids.split(StringPool.COMMA));
        return toResult(sysApplyService.removeByIds(list));
    }

    @Operation(summary = "查询角色应用树列表")
    @Parameter(name = "roleId", description = "角色ID")
    @GetMapping("/applyTreeSelect")
    public Result applyTreeSelect(Long roleId) {
        SysApply sysApply = new SysApply();
        sysApply.setUserId(SecurityUtils.getUserId());
        sysApply.setSysCode(SecurityUtils.getSysCode());
        Map<String, Object> map = new HashMap<>();
        map.put("checkedKeys", sysApplyService.selectApplyListById(roleId));
        map.put("applyList", sysApplyService.buildApplyTree(sysApply));
        return Result.success("查询成功", map);
    }

    @Operation(summary = "查询应用下拉树列表")
    @Parameters({
            @Parameter(name = "appName", description = "应用名称"),
            @Parameter(name = "status", description = "菜单状态(0正常 1停用)"),
    })
    @GetMapping("/treeSelect")
    public Result treeSelect(SysApply sysApply) {
        sysApply.setSysCode(SecurityUtils.getSysCode());
        return Result.success("查询成功", sysApplyService.buildApplyTree(sysApply));
    }

    @Operation(summary = "移动端查询应用列表树")
    @GetMapping("/getUserApplyList")
    public Result getUserApplyList(SysApply sysApply) {
        sysApply.setUserId(SecurityUtils.getUserId());
        sysApply.setSysCode(SecurityUtils.getSysCode());
        sysApply.setStatus(ApplyStatus.ENABLE.getCode());
        return sysApplyService.getUserApplyList(sysApply);
    }

}
